package com.Hwadee.educenter.controller;


import com.Hwadee.commonutils.JwtUtils;
import com.Hwadee.commonutils.R;
import com.Hwadee.commonutils.vo.UcenterOrderVo;
import com.Hwadee.educenter.entity.UcenterMember;
import com.Hwadee.commonutils.vo.CommentVo;
import com.Hwadee.educenter.entity.vo.RegisterVo;
import com.Hwadee.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author 677
 * @since 2021-07-09
 */
@RestController
@RequestMapping("/educenter/member")
//@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;


    //登录
    @PostMapping("/login")
    public R login(@RequestBody UcenterMember member) {
        //登录，返回token值，使用jwt实现
        String token = memberService.login(member);
        return R.ok().data("token", token);
    }


    //注册
    @PostMapping("/register")
    public R register(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    //改密
    @PostMapping("/repass")
    public R rePassword(@RequestBody RegisterVo registerVo) {
        memberService.rePassword(registerVo);
        return R.ok().data("code", 20000);
    }
    //根据token获取用户信息
    @GetMapping("/getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法，根据request对象获取头信息，返回用户ID
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据ID获取用户信息
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo", member);
    }

    //根据用户id查用户信息（评论）
    @GetMapping("/getInfo/{id}")
    public CommentVo getInfo(@PathVariable("id") String id) {
        UcenterMember ucenterMember = memberService.getById(id);
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(ucenterMember, commentVo);
        return commentVo;
    }

    //根据用户id查用户信息（订单）
    @GetMapping("/getUcenterOrder/{memberId}")
    public UcenterOrderVo getUcenterOrderInfo(@PathVariable("memberId") String memberId) {
        UcenterOrderVo ucenterOrderVo = new UcenterOrderVo();
        UcenterMember ucenterMember = memberService.getById(memberId);
        BeanUtils.copyProperties(ucenterMember, ucenterOrderVo);
        return ucenterOrderVo;
    }

    //统计某一天注册人数
    @GetMapping("/countRegister/{date}")
    public R countRegister(@PathVariable("date") String date) {
        Integer count = memberService.countRegister(date);
        return R.ok().data("countRegister", count);
    }

}

